//
//  SAPrintLableCell.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/7/6.
//

#import "SAPrintLableCell.h"
#import "SAPrintLableModel.h"

@interface SAPrintLableCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UIButton *selectBtn;
@property (nonatomic, strong) UIImageView *imageV;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UIButton *minusBtn;
@property (nonatomic, strong) UIButton *addBtn;
@property (nonatomic, strong) UITextField *numTf;
@property (nonatomic, strong) UILabel *contentLab;

@end

@implementation SAPrintLableCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

-(void)setModel:(SAPrintLableModel *)model {
    _model = model;
    self.titleLab.frame = SARectMake(105, 0, 165, 50);
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"print_bgImage"]];
    self.titleLab.text = [NSString stringWithFormat:@"%@,%@",model.productCode,model.productName];
    self.numTf.text = model.printNum;
    self.contentLab.hidden = YES;
    
    self.selectBtn.selected = model.isSelect;
    self.numTf.hidden = (model.isSelect != YES);
    self.addBtn.hidden = (model.isSelect != YES);
    self.minusBtn.hidden = (model.isSelect != YES);
}

-(void)setSingleModel:(SAPrintLableModel *)singleModel {
    _singleModel = singleModel;
    _model = singleModel;
    self.contentLab.hidden = NO;
    self.titleLab.frame = SARectMake(105, 5, 165, 25);
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:singleModel.img] placeholderImage:[UIImage imageNamed:@"print_bgImage"]];
    self.titleLab.text = [NSString stringWithFormat:@"%@,%@",singleModel.productCode,singleModel.productName];
    self.contentLab.text = [NSString stringWithFormat:@"%@,%@",singleModel.color,singleModel.size];
    self.numTf.text = singleModel.printNum;
    
    self.selectBtn.selected = singleModel.isSelect;
    self.numTf.hidden = (singleModel.isSelect != YES);
    self.addBtn.hidden = (singleModel.isSelect != YES);
    self.minusBtn.hidden = (singleModel.isSelect != YES);
}

#pragma mark - event
-(void)clickedSelectBtn {
    self.selectBtn.selected = !self.selectBtn.isSelected;
    self.model.isSelect = self.selectBtn.isSelected;
    !self.selectBlock ?: self.selectBlock();
    
    self.numTf.hidden = (self.model.isSelect != YES);
    self.addBtn.hidden = (self.model.isSelect != YES);
    self.minusBtn.hidden = (self.model.isSelect != YES);
}

-(void)clickedMinusBtn
{
    NSInteger num = [self.model.printNum integerValue] - 1;
    if (num < 0) {
        num = 0;
    }
    self.model.printNum = [NSString stringWithFormat:@"%zd",num];
    self.numTf.text = self.model.printNum;
    !self.countBlock ?: self.countBlock();
}

-(void)clickedAddBtn {
    NSInteger num = [self.model.printNum integerValue] + 1;
    if (num > 99) {
        num = 99;
    }
    self.model.printNum = [NSString stringWithFormat:@"%zd",num];
    self.numTf.text = self.model.printNum;
    !self.countBlock ?: self.countBlock();
}

- (void)textFieldDidChange:(UITextField *)textField {
    if (textField.text.length == 0) {
        self.model.printNum = @"";
    }else {
        NSInteger num = [textField.text integerValue];
        self.model.printNum = [NSString stringWithFormat:@"%zd",num];
        self.numTf.text = self.model.printNum;
    }
    !self.countBlock ?: self.countBlock();
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (string.length == 0) {
        return YES;
    }
    if (textField.text.length > 1) {
        return NO;
    }
    if ([textField.text isEqualToString:@"0"]) {
        textField.text = @"";
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self endEditing:YES];
   return YES;
}

#pragma mark - initUI
-(void)initUI {
    self.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.selectBtn];
    [self.contentView addSubview:self.imageV];
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.minusBtn];
    [self.contentView addSubview:self.addBtn];
    [self.contentView addSubview:self.numTf];
    [self.contentView addSubview:self.contentLab];
}

-(UIButton *)selectBtn {
    if (!_selectBtn) {
        _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _selectBtn.frame = SARectMake(0, 0, 62, 50);
        [_selectBtn setImage:[UIImage imageNamed:@"print_check_n"] forState:UIControlStateNormal];
        [_selectBtn setImage:[UIImage imageNamed:@"print_check_s"] forState:UIControlStateSelected];
        [_selectBtn addTarget:self action:@selector(clickedSelectBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectBtn;
}

-(UIImageView *)imageV {
    if (!_imageV) {
        _imageV = [[UIImageView alloc] initWithFrame:SARectMake(62, 6, 34, 38)];
        _imageV.image = [UIImage imageNamed:@"print_bgImage"];
        _imageV.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _imageV;
}

-(UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] initWithFrame:SARectMake(105, 0, 165, 50)];
        _titleLab.textColor = kBlackColor;
        _titleLab.font = kFont(15);
    }
    return _titleLab;
}

-(UILabel *)contentLab {
    if (!_contentLab) {
        _contentLab = [[UILabel alloc] initWithFrame:SARectMake(105, 25, 165, 20)];
        _contentLab.textColor = kLightgGrayColor;
        _contentLab.font = kFont(15);
    }
    return _contentLab;
}

-(UIButton *)minusBtn {
    if (!_minusBtn) {
        _minusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_minusBtn setImage:[UIImage imageNamed:@"print_minus"] forState:UIControlStateNormal];
        _minusBtn.frame = SARectMake(335, 0, 40, 50);
        _minusBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [_minusBtn addTarget:self action:@selector(clickedMinusBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _minusBtn;
}

-(UIButton *)addBtn {
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addBtn setImage:[UIImage imageNamed:@"print_add"] forState:UIControlStateNormal];
        _addBtn.frame = SARectMake(258, 0, 40, 50);
        _addBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_addBtn addTarget:self action:@selector(clickedAddBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}

-(UITextField *)numTf {
    if (!_numTf) {
        _numTf = [[UITextField alloc] initWithFrame:SARectMake(298, 0, 37, 50)];
        _numTf.textColor = kBlackColor;
        _numTf.textAlignment = NSTextAlignmentCenter;
        _numTf.font = kFont(15);
        _numTf.delegate = self;
        _numTf.keyboardType = UIKeyboardTypeNumberPad;
        _numTf.returnKeyType = UIReturnKeyDone;
        [_numTf addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _numTf;
}

@end
